﻿using AutoMapper;
using MediatR;
using Mysqlx.Crud;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.BasicInformation.warehouses;
using THE_WIND_RISES.Common.Domans.Produce.Resources;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Resourctes
{
    /// <summary>
    /// 资源管理添加
    /// </summary>
    public class ResourceCreateHandler : IRequestHandler<ResourceCreateCommend, Result>
    {
        private readonly IBaseRepository<Resource> Resource;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="Resource"></param>
        public ResourceCreateHandler(IMapper mapper, IBaseRepository<Resource> Resource)
        {
            this.mapper = mapper;
            this.Resource = Resource;
        }

        public async Task<Result> Handle(ResourceCreateCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() {Code=(int)CommonErrorCodes.Added };

            var AddData = mapper.Map<Resource>(request);
            string sql = "insert into Resource values(@ResourceId,@ResourceNo,@ResourceName,@ResourceTypeId,@ResourceDescription,@ResourceUnit,@ResourceCost,@ExpensesId,@ResourceCalculation,@BenchmarkId,@ResourcePrice,@ResourceState,@ApprovalStatus,@IsDelete,@Handle,@HandleContext,@HandleTime,@Pid);";
            var AddNum = Resource.Add(sql, new
            {
                ResourceId = AddData.ResourceId,
                ResourceNo = AddData.ResourceNo,
                ResourceName = AddData.ResourceName,
                ResourceTypeId = AddData.ResourceTypeId,
                ResourceDescription = AddData.ResourceDescription,
                ResourceUnit = AddData.ResourceUnit,
                ResourceCost = AddData.ResourceCost,
                ExpensesId = request.ExpensesId,
                ResourceCalculation = request.ResourceCalculation,
                BenchmarkId = request.BenchmarkId,
                ResourcePrice = request.ResourcePrice,
                ResourceState = AddData.ResourceState,
                ApprovalStatus = AddData.ApprovalStatus,
                Handle = AddData.Handle,
                HandleContext = AddData.HandleContext,
                HandleTime = AddData.HandleTime,
                Pid = AddData.Pid,
                IsDelete=AddData.IsDelete
            });
            if (AddNum > 0)
            {
                results.Code = (int)CommonErrorCodes.Success;
                return results;
            }
            else
            {
                return results;
            }
        }
    }
}
